package com.example.question.bt.backtrack1;

import java.util.ArrayList;
import java.util.List;

/**
 * @Date 2023-07-15
 * @Author Jonathan
 * @link {<a href="https://leetcode.cn/problems/permutations/">...</a>}
 */
public class Permutations2 {

    public static void main(String[] args) {
        Permutations2 code = new Permutations2();
        List<List<Integer>> res = code.permute(new int[]{1, 2, 3});
        System.out.println(res);
    }

    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        dfs(nums, new ArrayList<>(), res);
        return res;
    }


    /**
     * 深度优先遍历
     */
    private void dfs(int[] nums, List<Integer> path, List<List<Integer>> res) {
        // 结束递归的边界条件
        if (path.size() == nums.length) {
            res.add(new ArrayList<>(path));
            return;
        }

        path.add(nums[0]);
        dfs(nums, path, res);
        path.remove(path.size() - 1);

        path.add(nums[1]);
        dfs(nums, path, res);
        path.remove(path.size() - 1);

        path.add(nums[2]);
        dfs(nums, path, res);
        path.remove(path.size() - 1);
    }
}
